package com.liusaidh.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.liusaidh.domain.*;
import com.liusaidh.result.Result;
import com.liusaidh.result.Status;
import com.liusaidh.mapper.BlogMapper;
import com.liusaidh.mapper.UserMapper;
import com.liusaidh.service.BlogService;
import com.liusaidh.utils.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;

@Service
public class BlogServiceImpl implements BlogService {
    @Autowired
    private BlogMapper blogMapper;
    @Autowired
    private UserMapper userMapper;


    @Override
    public PageBean getReportForManager(Integer pageNo, Integer pageSize) {
        pageNo = pageNo > 0 ? pageNo : 1;
        pageSize = pageSize > 0 ? pageSize : 10;
        return new PageBean(blogMapper.getAllReportCount(), blogMapper.getReportForManager((pageNo - 1) * pageSize, pageSize));
    }

    @Override
    public AuditBlog getAuditBlogById(Integer id) {
        return blogMapper.getAuditBlogById(id);
    }

    @Override
    public List<AuditBlog> getMyLikeBlogList(Integer id, Integer pageNo, Integer pageSize) {
        return blogMapper.getMyLikeBlogList(id, (pageNo - 1) * pageSize, pageSize);
    }

    @Override
    public List<AuditBlog> getMyCollectionBlogList(Integer id, Integer pageNo, Integer pageSize) {
        return blogMapper.getMyCollectionBlogList(id, (pageNo - 1) * pageSize, pageSize);
    }

    @Override
    public void addBlog(Blog blog, String[] type) {
        blog.setAmount(0);
        blog.setExist(0);
        blog.setState(0);
        blog.setTransmit(0);
        blog.setLikes(0);
        blog.setHot(1);
        blog.setPublishedDate(LocalDateTime.now().toString());
        blogMapper.addBlog(blog);
        if (blog.getLabel() != null) {
            Integer id = blogMapper.getLabelIdByLabel(blog.getLabel());
            if (id == null) {
                blogMapper.addNewLabel(blog.getLabel(), LocalDateTime.now());
                Integer labelId = blogMapper.getLabelIdByLabel(blog.getLabel());
                blogMapper.addLabelForBlog(blog.getId(),labelId);
            } else {
                blogMapper.addLabelForBlog(blog.getId(), id);
            }
        }
        if (type != null && type.length != 0) {
            Integer[] ids = blogMapper.getTypeIds(type);
            System.out.println(Arrays.toString(ids));
            blogMapper.addTypeForBlog(blog.getId(), ids);
        }
        if (blog.getPictureUrl() != null && blog.getPictureUrl().length != 0) {
            blogMapper.addPhotoUrl(blog.getId(), blog.getPictureUrl());
        }
    }

    @Transactional
    @Override
    public Result likeBlog(Integer uid, Integer bid) {
       try{
           blogMapper.likeBlog(uid, bid, RandomUtil.getLocalTime());
           blogMapper.addOneLikeToBlog(bid);
       }catch (Exception e){
           return Result.fail(Status.FAIL);
       }
       return Result.success(Status.OK);
    }

    @Override
    public void notLikeBlog(Integer uid, Integer bid) {
        blogMapper.notLikeBlog(uid, bid);
        blogMapper.deleteOneLikeToBlog(bid);
    }


    @Override
    public PageBean getAuditBlogList(Integer pageNo, Integer pageSize) {
        pageNo = pageNo > 0 ? pageNo : 1;
        pageSize = pageSize > 0 ? pageSize : 10;
        return new PageBean(blogMapper.getAuditBlogCount(), blogMapper.getAuditBlogList((pageNo - 1) * pageSize, pageSize));
    }

    @Override
    public List<String> getTenHotSearchBlog() {
        return blogMapper.getTenHotSearchBlog();
    }


    @Override
    public List<String> getSearchBlogByLike(String condition) {
        return blogMapper.getSearchBlogByLike(condition);
    }


    @Override
    public List<AuditBlog> getAllBlog(Integer pageNo, Integer pageSize) {
        return blogMapper.getAllBlog((pageNo - 1) * pageSize, pageSize);
    }

    @Override
    public String[] getBlogPictureUrl(Integer id) {
        return blogMapper.getBlogPictureUrl(id);
    }

    @Override
    public void addBlogToDraftBox(Blog blog) {
//        new Blog(0,0,0,0,1,2,LocalDate.now().toString());
        blog.setAmount(0);
        blog.setExist(0);
        blog.setTransmit(0);
        blog.setLikes(0);
        blog.setHot(1);
        blog.setState(2);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        blog.setPublishedDate(LocalDateTime.now().format(formatter));
        blogMapper.addBlog(blog);
        if (blog.getPictureUrl().length != 0) {
            blogMapper.addPhotoUrl(blog.getId(), blog.getPictureUrl());//传入图片路径数组
        }
    }

    @Override
    public void collectOtherBlog(Integer uid, Integer bid, String msg) {
        blogMapper.collectOtherBlog(uid, bid, msg);
    }

    @Override
    public void notCollectOtherBlog(Integer uid, Integer bid) {
        blogMapper.notCollectOtherBlog(uid, bid);
    }

    @Override
    public PageBean getHotBlogForManager(Integer pageNo, Integer pageSize) {
        pageNo = pageNo > 0 ? pageNo : 1;
        pageSize = pageSize > 0 ? pageSize : 10;
        return new PageBean(blogMapper.getAllLabelCount(), blogMapper.getHotBlogForManager((pageNo - 1) * pageSize, pageSize));
    }

    @Override
    public int deleteHotSearchBlog(String label) {
        return blogMapper.deleteHotSearchBlog(label);
    }

    @Override
    public List<String> getHotBlog() {
        return blogMapper.getFiftyHotSearchBlog();
    }

    @Override
    public boolean judgeBlogOfLike(Integer myId, Integer blogId) {
        return blogMapper.judgeBlogOfLike(myId, blogId) == 1;
    }

    @Override
    public boolean judgeBlogOfCollection(Integer myId, Integer blogId) {
        return blogMapper.judgeBlogOfCollection(myId, blogId) == 1;
    }

    @Override
    public boolean judgeBlogOfTransmit(Integer myId, Integer blogId) {
        return blogMapper.judgeBlogOfTransmit(myId, blogId) == 1;
    }

    @Override
    public int notPassTheAudit(Integer id) {
        return blogMapper.notPassTheAudit(id);
    }

    @Override
    public List<AuditBlog> getTheLatestHotBlog(Integer pageNo,Integer pageSize) {
        return blogMapper.getTheLatestHotBlog(2,(pageNo - 1)*pageSize,pageSize);
    }

    @Override
    public List<AuditBlog> getSearchAuditBlogByLike(String label,Integer pageNo,Integer pageSize) {
        return blogMapper.getAuditSearchBlogByLike(label,(pageNo - 1)*pageSize,pageSize);
    }

    @Override
    public List<String> getTypeByLike(String condition) {
        return blogMapper.getTypeByLike(condition);
    }

    @Override
    public List<MyBlog> getAllMyBlog(Integer id, Integer pageNo, Integer pageSize) {
        return blogMapper.getAllMyBlog(id, (pageNo - 1) * pageSize, pageSize);
    }



    @Override
    public List<AuditBlog> getTheLatestHotBlogForLogin() {
        return blogMapper.getTheLatestHotBlog(5,1,20);
    }


//    @Override
//    public List<AuditBlog> getMyBlog(Integer id) {
//        //这里还要加上一个获取我转发的文章的功能
//        return getAllMyBlog(id);
//    }

    @Override
    public List<AuditBlog> getMyDraftBox(Integer id) {
        return blogMapper.getMyDraftBox(id);
    }

    @Override
    public int passTheAudit(Integer id) {
        return blogMapper.passTheAudit(id);
    }

    @Override
    public Result publishFirstComment(Integer uid, Integer bid, String comment) {
        try{
            int i = blogMapper.publishFirstComment(uid, bid, comment, LocalDateTime.now());
        }catch (Exception e){
            return Result.fail(Status.FAIL);
        }
        return Result.success(Status.OK);
    }

    @Override
    public int publishSecondComment(Integer uid, Integer firstCommentId, String comment) {
        return blogMapper.publishSecondComment(uid, firstCommentId, comment, LocalDate.now());
    }

    @Override
    public List<UserReviews> getBlogComment(Integer id) {
        return blogMapper.getBlogComment(id);
    }

    @Override
    public List<AuditBlog> getMyConnectionBlog(Integer id,Integer pageNo, Integer pageSize) {
        return blogMapper.getMyCollectionBlog(id,(pageNo - 1)*pageSize,pageSize);
    }

    @Override
    public int forwardTheBlog(Integer uid, Integer bid, String message) {
        blogMapper.addOneTransmitForBlog(bid);
        return blogMapper.forwardTheBlog(uid, bid, message, LocalDateTime.now().toString());
    }


    @Override
    public PageBean getAllTypeForManager(Integer pageNo, Integer pageSize, String type) {
        pageNo = pageNo > 0 ? pageNo : 1;
        pageSize = pageSize > 0 ? pageSize : 10;
        return new PageBean(blogMapper.getAllTypeCount(type), blogMapper.getAllTypeForManager((pageNo - 1) * pageSize, pageSize, type));
    }


    @Override
    public Long getAllTypeCount() {
        return blogMapper.getAllTypeCount(null);
    }

    @Override
    public int deleteType(Integer id) {
        return blogMapper.deleteType(id);
    }

    @Override
    public int addType(String type) {
        return blogMapper.addType(type);
    }

    @Override
    public List<AuditBlog> getMyLatestConnectionBlog(Integer id,Integer pageNo,Integer pageSize) {
        return blogMapper.getMyLatestConnectionBlog(id,(pageNo - 1)*pageSize,pageSize);
    }

    @Override
    public List<AuditBlog> getTheLoadedBlog(Integer pageNo, Integer pageSize) {
        pageNo = pageNo > 0 ? pageNo : 1;
        pageSize = pageSize > 0 ? pageSize : 10;
        return blogMapper.getTheLoadedBlog((pageNo - 1) * pageSize, pageSize);
    }

    @Override
    public Result deleteTheBlog(Integer id) {
        if (blogMapper.deleteBlogById(id) <= 0){
            return Result.fail(Status.OK,"删除失败！");
        }
        return Result.success(Status.OK,"删除成功！");
    }

    @Override
    public Result likeComment(Integer uid, Integer commentId) {
        try {
            blogMapper.addOneLikeForComment(commentId);
            blogMapper.likeComment(uid, commentId, LocalDateTime.now());
        } catch (Exception e) {
            return Result.fail(Status.FAIL);
        }
        return Result.success(Status.OK);
    }

    @Override
    public int notLikeComment(Integer uid, Integer commentId) {
        blogMapper.deleteOneLikeForComment(commentId);
        return blogMapper.notLikeComment(uid, commentId);
    }

    @Override
    public boolean judgeCommentOfLike(Integer uid, Integer commentId) {
        return blogMapper.judgeCommentOfLike(uid, commentId) == 1;
    }

    @Override
    public Result reportBlog(Integer uid, Integer bid, String msg) {
            try{
                blogMapper.reportBlog(uid, bid, msg, RandomUtil.getLocalTime());
            }catch (Exception e){
                return Result.fail(Status.FAIL);
            }
        return Result.success(Status.OK);
    }



    @Override
    public int deleteComment(Integer id) {
        return blogMapper.deleteComment(id);
    }

    @Override
    public List<String> getLabelByLike(String condition) {
        return blogMapper.getLabelByLike(condition);
    }

    @Override
    public PageBean getLabelForManager(Integer pageNo, Integer pageSize) {

        long totalCount = blogMapper.getLabelTotal();
        int pageMaxSize = (int) totalCount / pageSize;
        pageNo = pageNo < 1 ? 1 : pageNo;
        if (totalCount % pageSize == 0) {
            if (pageNo > pageMaxSize) {
                pageNo = pageMaxSize;
            }
        } else {
            if (pageNo > pageMaxSize + 1) {
                pageNo = pageMaxSize + 1;
            }
        }
        pageSize = pageSize > 0 ? pageSize : 10;
        Integer pageBeginIndex = (pageNo - 1) * pageSize;
        List<Label> list = blogMapper.getLabelForManager(pageBeginIndex, pageSize);
        return new PageBean(blogMapper.getLabelTotal(), list);
    }

    @Override
    public int deleteHotLabel(Integer id) {
        return blogMapper.deleteHotLabel(id);
    }

    @Override
    public int passReport(Integer id) {
        return blogMapper.passReport(id);
    }

    @Override
    public int notPassReport(Integer id) {
        return blogMapper.notPassReport(id);
    }

    @Override
    public PageBean testPageHelper(Integer pageNo, Integer pageSize,Integer condition) {
        PageHelper.startPage(pageNo, pageSize);
        List<User> userList = blogMapper.test(condition);
        Page<User> page = (Page<User>) userList;
        return new PageBean(page.getTotal(),page.getResult());
    }
}